using UnityEngine;
using System.Collections;

public class BallControl : MonoBehaviour 
{
	public float moveSpeed = 50.0f;
	private bool isTouchDown = false;
	
	void Update() 
	{
		Vector3 move = Vector3.zero;

		if (Input.acceleration.magnitude != 0.0f)
			move = (-Input.acceleration.y * Vector3.right * moveSpeed * 4.0f) + (Input.acceleration.x * Vector3.forward * moveSpeed * 4.0f);
		else
			move = (Input.GetAxis("Horizontal") * -Vector3.left * moveSpeed) + (Input.GetAxis("Vertical") * Vector3.forward * moveSpeed);

        rigidbody.AddForce(move, ForceMode.Force);
		
		if (((Input.touchCount > 0) || Input.GetKeyDown(KeyCode.Space)) && !isTouchDown)
		{
			isTouchDown = true;
			rigidbody.AddForce(Vector3.up * 2000.0f, ForceMode.Force);
		}
		else if (Input.touchCount == 0 && isTouchDown)
		{
			isTouchDown = false;
		}
		
		if (transform.position.y < -5.0f)
		{
			transform.position = new Vector3(2.0f, 0.55f, 2.0f);
			rigidbody.velocity = Vector3.zero;
		}
	}
	
	void OnTriggerEnter(Collider other)
	{
		if (other.tag == "Coin")
		{
			GameManager.GM.FoundCoin();
			other.transform.position = new Vector3(Random.Range(1, 49), other.transform.position.y, Random.Range(1, 49));
		}
	}
}
